<?php

/*

  Action - Attack	$2 + 1 potion

  +1 Action
  Each player (including you) reveals the top card of his deck and either discards it or puts it back, your choice.
  Then reveal cards from the top of your deck until revealing one that isn’t an Action.  Put all of your revealed cards into your hand.

 */

class ScryingPool extends Card implements iAction, iKingdom, iAttack, iAlchemy {

    public function __construct() {
        $price = new Price(2, 1);
        parent::__construct("Scrying Pool", $price);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+1 Action",
            "Each player (including you) reveals the top card of his deck and either discards it or puts it back, your choice.  Then reveal cards from the top of your deck until revealing one that isn’t an Action.  Put all of your revealed cards into your hand.");
    }

    private function helper(Game $game, $name) {

        $card = $game->removeCardFromDrawDeck($name);
        if ($card) {
            ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($card), "reveals " . ScreenDisplay::$LMSG, $name);
            $options = array(new Option(1, "Yes"), new Option(2, "No"));

            $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Discard " . $card->getValue() . " to " . $name . "?");
            if ($output[0] == 'Yes') {
                $game->putCardInDiscardDeck($card, $name);
                 ScreenDisplay::addScreenMessage($game, false, " and discard it", " and discards it", $name);
            } else {
                $game->putCardOnTopOfDrawDeck($card, $name);
                 ScreenDisplay::addScreenMessage($game, false, " and returns it on top of your deck", " and returns it on top of his deck", $name);
            }
        }
    }

    public function play(Game $game, $isRealCard) {

        $game->addActions(1);
        $game->attack($this);

        $names = $game->getPlayersNames(true);
        foreach ($names as $name) {
            $this->helper($game, $name);
        }

        $card = $game->peekCardFromDrawDeck();
        $drawed_cards = array();
        while ($card != null && $card instanceof iAction) {
            $temp = $game->drawCards(1);
            $drawed_cards = array_merge($temp, $drawed_cards);
            $card = $game->peekCardFromDrawDeck();
        }
        $temp = $game->drawCards(1);
        $drawed_cards = array_merge($temp, $drawed_cards);

        if (sizeof($drawed_cards) > 1)
             ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($drawed_cards) . " and put them in your hand", "reveals " . ScreenDisplay::$LMSG . " and put them in his hand");
        else if (sizeof($drawed_cards) == 1)
             ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($drawed_cards) . " and put it in your hand", "reveals " . ScreenDisplay::$LMSG . " and put it in his hand");
        else if (sizeof($drawed_cards) < 1)
             ScreenDisplay::addScreenMessage($game, true, "reveal nothing", "reveals nothing");

        $game->cardActionFinished();
        return true;
    }
    
        public function boardSetup(Game $game) {
        $game->addPotion();
        return true;
    }

}

?>